<?php

namespace app\admin\controller;

use think\Controller;
use think\Validate;

class User extends Controller
{
    public function ordinary()
    {
        $arr = db('user')->where(['is_party' => 1, 'is_volunteer' => 1, 'del_status' => 1])->paginate(10, false, ['query' => request()->param()]);
        $pag = $arr->render();
        $this->assign('pag', $pag);
        $this->assign('arr', $arr);
        return $this->fetch();
    }
    public function publish()
    {
        if (request()->isAjax()) {
            $a = input('post.');
            $z = db('user')->where(['phone' => $a['phone']])->find();
            if (empty($z)) {
                $post = input('post.');
                $val = [
                    'shenfenzheng' => 'max:18|number',
                    'phone' => 'number|/^1[3-8]{1}[0-9]{9}$/',
                    'password' => 'max:6'
                ];
                $may = [
                    'shenfenzheng.max' => '身份证必须是18位组成',
                    'shenfenzheng.number' => '身份证只能是数字',
                    'phone.number' => '电话只能是数字',
                    'phone./^1[3-8]{1}[0-9]{9}$/' => '请输入正确的手机号',
                    'password.max' => '密码最最低是6位'
                ];
                $validate = new Validate($val, $may);
                $is = $validate->check($post);
                if (!$is) {
                    $this->error($validate->getError());
                };
                $whe['name'] = trim($post['name']);
                $whe['phone'] = trim($post['phone']);
                $whe['password'] = md5($post['password']);
                $whe['idcard'] = trim($post['shenfenzheng']);
                $whe['ads_xiaoqu'] = trim($post['xiaoqu']);
                $whe['ads_louhao'] = trim($post['lou']);
                $whe['ads_danyuan'] = trim($post['danyuan']);
                $whe['ads_menpai'] = trim($post['men']);
                $whe['integral'] = 0;
                $whe['ads_type'] = trim($post['article_cate_id']);
                $isok = db('user')->insert($whe);
                $arr = [];
                if ($isok) {
                    $arr['code'] = 1;
                } else {
                    $arr['code'] = 2;
                }
                return $arr;
            } else {
                $this->error('手机号重复！');
            };
        }
        return $this->fetch();
    }
    public function delete()
    {
        if (request()->isAjax()) {
            $id = input('id');
            $isok = db('user')->where('id', $id)->update(['del_status' => 2]);
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
            } else {
                $arr['code'] = 2;
            }
            return $arr;
        }
    }
    public function reform()
    {
        $id = input('id');
        $list = db('user')->where('id', $id)->find();
        $this->assign('list', $list);
        return $this->fetch();
    }
    public function party()
    {
        $arr = db('user')->where(['is_party' => 2, 'is_volunteer' => 1, 'del_status' => 1])->paginate(10, false, ['query' => request()->param()]);
        $pag = $arr->render();
        $this->assign('pag', $pag);
        $this->assign('arr', $arr);
        return $this->fetch();
    }
    public function del()
    {
        if (request()->isAjax()) {
            $id = input('id');
            $isok = db('user')->where('id', $id)->update([
                'is_party' => 1
            ]);
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
            } else {
                $arr['code'] = 2;
            };
            return $arr;
        }
    }
    public function pulishtd()
    {
        $list = db('user')->where(['del_status' => 1, 'is_party' => 1])->select();
        $this->assign('list', $list);
        if (request()->isAjax()) {
            $a = input('post.');
            $arr = db('user')->where('id', $a['admin_cate_id'])->update(['is_party' => 2]);
            if ($arr == 1) {
                return $this->success('提交成功', 'admin/user/party');
            } else {
                return $this->error('提交失败');
            }
        }
        return $this->fetch();
    }
    public function audit()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($id > 0) {
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                $status = $post['status'];
                if (false == db('user')->where('id', $id)->update(['user_status' => $status])) {
                    return $this->error('审核提交失败');
                } else {
                    return $this->success('审核提交成功', 'admin/user/ordinary');
                }
            }
        } else {
            return $this->error('id不正确');
        }
    }
    public function red()
    {
        $arr = db('user')->where(['is_party' => 2, 'is_volunteer' => 2, 'del_status' => 1])->paginate(10, false, ['query' => request()->param()]);
        $pag = $arr->render();
        $this->assign('pag', $pag);
        $this->assign('arr', $arr);
        if (request()->isAjax()) {
            $id = input('id');

            if (false == db('user')->where('id', $id)->update(['is_volunteer' => 1])) {
                return $this->error('降级失败');
            } else {
                return $this->success('降级成功', 'admin/user/red');
            };
        }
        return $this->fetch();
    }
    public function publishred()
    {
        $list = db('user')->where(['del_status' => 1, 'is_party' => 2, 'is_volunteer' => 1])->paginate(10, false, ['query' => request()->param()]);
        $pag = $list->render();
        $this->assign('pag', $pag);
        $this->assign('list', $list);
        if (request()->isAjax()) {
            $a = input('post.');
            $arr = db('user')->where('id', $a['admin_cate_id'])->update(['is_volunteer' => 2]);
            if ($arr == 1) {
                return $this->success('提交成功', 'admin/user/red');
            } else {
                return $this->error('提交失败');
            }
        }
        return $this->fetch();
    }
    public function property()
    {
        $arr = db('wuye')->where('yong_status', 1)->paginate(10, false, ['query' => request()->param()]);
        $pag = $arr->render();
        $this->assign('pag', $pag);
        $this->assign('arr', $arr);
        return $this->fetch();
    }
    public function publishty()
    {
        if (request()->isAjax()) {
            $a = input('post.');
            $z = db('wuye')->where(['phone' => $a['phone']])->find();
            if (empty($z)) {
                $post = input('post.');
                $val = [
                    'phone' => 'number|/^1[3-8]{1}[0-9]{9}$/',
                    'password' => 'min:6'
                ];
                $may = [
                    'phone.number' => '电话只能是数字',
                    'password.max' => '密码最最低是6位',
                    'phone./^1[3-8]{1}[0-9]{9}$/' => '请输入有效的手机号'
                ];
                $validate = new Validate($val, $may);
                $is = $validate->check($post);
                if (!$is) {
                    $this->error($validate->getError());
                };
                $whe['name'] = trim($post['name']);
                $whe['phone'] = trim($post['phone']);
                $whe['password'] = md5($post['password']);
                $whe['xiaoqu'] = trim($post['xiaoqu']);
                $isok = db('wuye')->insert($whe);
                $arr = [];
                if ($isok) {
                    $arr['code'] = 1;
                } else {
                    $arr['code'] = 2;
                }
                return $arr;
            } else {
                $this->error('手机号重复！');
            };
        }
        return $this->fetch();
    }
    public function det()
    {
        if (request()->isAjax()) {
            $id = input('id');
            $isok = db('wuye')->where('id', $id)->update(['yong_status' => 2]);
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
            } else {
                $arr['code'] = 2;
            }
            return $arr;
        }
    }
    public function aud()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($id > 0) {
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                $status = $post['status'];
                if (false == db('wuye')->where('id', $id)->update(['status' => $status])) {
                    return $this->error('审核提交失败');
                } else {
                    return $this->success('审核提交成功', 'admin/user/property');
                }
            }
        } else {
            return $this->error('id不正确');
        }
    }
    public function change()
    {
        $id = input('id');
        $info = db('wuye')->where('id', $id)->find();
        $this->assign('info', $info);
        if (request()->isAjax()) {
            $post = input('post.');
            $map = [];
            $map['phone'] = ['eq', $post['phone']];
            $map['id'] = ['neq', $post['aid']];
            $arr = db('wuye')->where($map)->find();
            if (empty($arr)) {
                $isok = db('wuye')->where('id',$post['aid'])->update([
                    'name' => $post['name'],
                    'phone' => $post['phone'],
                    'password' => md5($post['password']),
                    'xiaoqu' => $post['xiaoqu']
                ]);
                if($isok == 1){
                    $this->success('修改成功！');
                }else{
                    $this->error('修改失败！');
                };                 
            } else {
                $this->error('手机号重复');
            }
        }
        return $this->fetch();
    }
}
